package com.jmyd.txlcn.domain;

import com.jmyd.txlcn.exception.CustomException;
import com.jmyd.txlcn.util.IdGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * @author samphin
 * @date 2020/12/14 19:54
 */
@Slf4j
public class DomainPoFactory {

    /**
     * 创建po对象
     *
     * @param entityClass 需要创建的对象
     * @author samphin
     * @date 2020-12-14 20:11:42
     */
    public static <T> T newInstance(Class<T> entityClass) {
        return newInstance(entityClass, IdGen.uuid(), Boolean.TRUE);
    }

    /**
     * 创建po对象（不包含主键）
     *
     * @param entityClass 需要创建的对象
     * @author samphin
     * @date 2020-12-14 20:11:42
     */
    public static <T> T newInstance(Class<T> entityClass, boolean isUseCreateBy) {
        try {
            T newInstance = entityClass.newInstance();
            //继承BaseStandardPo、BaseSimpleStandardPo、BasePo类的字段列表进行初始化
            Field[] fields = newInstance.getClass().getFields();
            Date operateDate = new Date();
            for (Field field : fields) {
                if ("createTs,updateTs".contains(field.getName())) {
                    field.set(newInstance, operateDate);
                } else if ("createdBy,updatedBy".contains(field.getName()) && isUseCreateBy) {
                    field.set(newInstance, null);
                }
            }
            return newInstance;
        } catch (InstantiationException | IllegalAccessException e) {
            log.error(e.getMessage(), e);
        }
        throw new CustomException("对象创建失败...");
    }

    /**
     * 创建po对象（包含主键）
     *
     * @param entityClass 需要创建的对象
     * @param id          自定义id
     * @author samphin
     * @date 2020-12-14 20:11:42
     */
    public static <T> T newInstance(Class<T> entityClass, Object id, boolean isUseCreateBy) {
        try {
            T newInstance = entityClass.newInstance();
            Class<?> aClass = newInstance.getClass();
            Field[] fields = aClass.getFields();
            if (ArrayUtils.isEmpty(fields)) {
                fields = aClass.getDeclaredFields();
            }
            Date operateDate = new Date();
            for (Field field : fields) {
                if ("id".equals(field.getName())) {
                    field.setAccessible(true);
                    field.set(newInstance, id);
                } else if ("createTs,updateTs".contains(field.getName())) {
                    field.setAccessible(true);
                    field.set(newInstance, operateDate);
                } else if ("createdBy,updatedBy".contains(field.getName()) && isUseCreateBy) {
                    field.setAccessible(true);
                    field.set(newInstance, null);
                }
            }
            return newInstance;
        } catch (InstantiationException | IllegalAccessException e) {
            log.error(e.getMessage(), e);
        }
        throw new CustomException("对象创建失败...");
    }

    /**
     * @param entityClass 需要创建的对象，默认需要创建人、修改人
     * @author samphin
     * @date 2020-12-14 20:11:42
     */
    public static <T> T newInstance(Class<T> entityClass, Object id) {
        return newInstance(entityClass, id, Boolean.TRUE);
    }
}
